package com.jy.api.controller.admin;

import com.jy.api.service.OrderService;
import com.jy.api.service.ProductService;
import com.jy.api.service.ProductTypeService;
import com.jy.api.vo.admin.payment.PaymentVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

/**
 * Created by lihu on 2017/12/16.
 */

@Slf4j
@RestController
@RequestMapping(value = "v1/admin", produces = "application/json;charset=utf-8")
public class AdminPaymentController {


    @Autowired
    ProductTypeService productTypeService;
    @Autowired
    private ProductService productService;
    @Autowired
    private OrderService orderService;

    /**
     * 63+
     * <p>
     * 查询时间端销售统计
     *
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/{storeId}/sales-total")
    public ResponseEntity getSalesTotal(@PathVariable String storeId,
                                        @RequestParam(name = "productTypeId", defaultValue = "") String typeId,
                                        @RequestParam(name = "productId", defaultValue = "") String productId,
                                        @RequestParam(name = "from") String from,
                                        @RequestParam(name = "to") String to) {
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            simpleDateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
            Date start = simpleDateFormat.parse(from);
            Date end = simpleDateFormat.parse(to);
            List<String> typeIds = new ArrayList<>();
            if (StringUtils.isNotBlank(typeId) && StringUtils.isBlank(productId)) {
                typeIds = productTypeService.getTypeIds(typeId);
            }
            return ResponseEntity.ok(new PaymentVo());
        } catch (Exception ex) {
            log.error("getSalesTotal is error=>storeId:{}", storeId, ex);
            return new ResponseEntity("系统运行错误", HttpStatus.INTERNAL_SERVER_ERROR);
        }

    }

    /**
     * admin 通过时间/订单号查询 对账单
     *
     * @param storeId
     * @param time
     * @return
     */
    @GetMapping("get-storeBill")
    public ResponseEntity getStoreBill(@RequestParam(value = "begin",required = false)String begin,@RequestParam(value = "empMobile",required = false)String empMobile,@RequestParam(value = "end",required = false)String end,@RequestParam(value = "type",required = false)String type,@RequestParam(name = "source", required = false) String source, @RequestParam(name = "storeId", required = false) String storeId, @RequestParam(name = "orderId", required = false) String orderId, @RequestParam(name = "time", required = false) String time, @RequestParam(name = "workShiftId", required = false) String workShiftId,@RequestParam(value = "productName",required = false)String productName, @PageableDefault(sort = {"createTime"}, direction = Sort.Direction.DESC) Pageable pageable) {
        return new ResponseEntity(productService.findStoreBill(begin,end,type,orderId, storeId, time, source, workShiftId,empMobile,pageable,productName), HttpStatus.OK);
    }


    @GetMapping("get-item")
    public ResponseEntity getStoreBillByOrderId(@RequestParam("orderId") String orderId) {
        return ResponseEntity.ok(orderService.getStoreBillByOrderId(orderId));
    }


}
